//*****************************************************************************
//  172x72 COG Display Temp. Comp. FW
//  MCU: MSP430F235
//  PCB: 10424
//  Compiler: TI Code Composer Essentials MSP430 v3.1
//  Kent Displays, Inc.
//  Copyright (c) 2009. All rights reserved worldwide.
//  Revisions
//     A - Initial. (24V Drive System, No Focal Conic Erase)
//     B - 28V Drive System, Focal Conic Erase
//*****************************************************************************

#include "defines.h"
#include "system.h"
#include "display.h"
#include "image.h"
#include "cal.h"
#include "GT22.h"
#include "handl.h" 
#include "CC2500.h"
#include "led.h" 
#include "rtc.h"
#include "hal_ucs.h"
#include "flashUtils.h"

/********** reply *************/

#define REPLY_OK 0x00
#define REPLY_ERR 0x01
#define REPLY_LOSE 0x03

enum { UPDATETYPE = 0x64, TIMETYPE = 0x65, INTERVALTYPE = 0x66 , WORKTIMETYPE = 0x67, ESLADDRESSTYPE = 0x68 };
unsigned char MessageType;

typedef enum {  LPM4_MODE, LPM3_MODE, ACTIVE_MODE, APPLICATION_MODE } McuStatus;
volatile McuStatus CpuMode = ACTIVE_MODE;

typedef enum {  TURE = 0, FALSE = !TURE } FlagStatus;

volatile FlagStatus time = TURE;

volatile FlagStatus interrup_time = TURE;

volatile FlagStatus receive_flag = TURE;

unsigned char active_time_count = 0x00;

unsigned char TxBuf_OK[10] = { 0x09, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x00, 0x92 };
unsigned char TxBuf_ERR[10] = { 0x09, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x01, 0x91 };
unsigned char TxBuf_LOSE[10] = {0x0a, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

// Global variables.
volatile unsigned int gTimer;   // Used to extend HW 16 bit counter to 32 bits.

unsigned char rxBuffer[64];

volatile Calendar newTime;

//void setupTime(Calendar currentTime);

volatile unsigned char count = 0x00;

void main(void)
{
	volatile unsigned int i = 0;
    volatile unsigned int j = 0;  
//    Calendar currentTime;
    
	// System Initialization
	StopWatchdog();               // Stop watchdog timer.
	ConfigPorts();                // Configure I/O ports.
//	ConfigClocks();               // Configure basic clock module.
    
    P5SEL |= BIT4 + BIT5;                       // Port select XT1
 
	//Initialize clock and peripherals
	LFXT_Start(XT1DRIVE_3, XCAP_3);
    Init_FLL_Settle(8000, 244);
    SFRIE1 |= OFIE;
    
    GT_config();
    LED_config();

    SPI_SELECT();
    SPI_SET_CLK();
    SPI_DIS_SELECT();
    cc2500_Init(0x00);
    Set_CCxx00Rxd();

//	setupRTC();
    
    P2IES |= 0x01;
    P2IFG &= ~0x01;
    P2IE  |= 0x01;
    
    _BIS_SR(GIE);
	
	CpuMode = ACTIVE_MODE;

//	__bis_SR_register(LPM3_bits + GIE); // Enter LPM3

    while(1) {  
        switch (CpuMode)
		{
		case ACTIVE_MODE:
			if ((receive_flag == FALSE)) {
                receive_flag = TURE;
                
                if (fcc_crc(rxBuffer)) {
                }
                
                Set_CCxx00Rxd();
                P2IFG &= ~BIT0;
                P2IE |= 0x01;
			} 	
			break;
		}
//        __bis_SR_register(LPM0_bits + GIE); // Enter LPM3
    }
}

#pragma vector =PORT2_VECTOR
__interrupt void P2_sever()
{
    P2IE  &= ~BIT0;
    P2IFG &= ~BIT0;
    RFReceivePacket(rxBuffer);

    LED1_toggle();
    
    if (receive_flag == TURE) {
        receive_flag = FALSE;
    } else {
        receive_flag = TURE;
        Delay(10);
        Set_CCxx00Rxd();
        P2IE |= 0x01;
    }
}
